package com.example.larouzey.recyclerview_201ljy.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.larouzey.recyclerview_201ljy.adpater.MyAdapter;
import com.example.larouzey.recyclerview_201ljy.R;
import com.example.larouzey.recyclerview_201ljy.bean.ArticleBean;
import com.example.larouzey.recyclerview_201ljy.iface.ArticleListener;
import com.example.larouzey.recyclerview_201ljy.model.ArticleModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;//RecyclerView 控件
    private RecyclerView.LayoutManager lm;//RecyclerView控件布局
    private MyAdapter ma;//RecyclerView
    private List<ArticleBean> list = null;

    ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list=beanList;//获取文章
            ma.setList(list);
            ma.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,"失败："+msg,Toast.LENGTH_SHORT);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();

        ArticleModel model=new ArticleModel();
        model.getResultList("article",1,"rv1l2tidbomann2qn86nln6ur2",articleListener);
    }
    private void initRecyclerView(){
        rv = (RecyclerView)findViewById(R.id.recyclerview);
        //创建默认线性布局
        lm= new LinearLayoutManager(this);
        //设置布局管理器
        rv.setLayoutManager(lm);
        //固定每个item高度，可以提高性能
        rv.setHasFixedSize(true);
        //获取数据
       // list=getData();
        //实例化Adapter
        ma= new MyAdapter(this);
        //传进数据
        ma.setList(list);
        //绑定RecyclerView和Adaper
        rv.setAdapter(ma);

    }
    /*private List<ArticleBean> getData(){
        List<ArticleBean> list= new ArrayList<ArticleBean>();
        ArticleBean articleBean = new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);

        ArticleBean articleBean2 = new ArticleBean();
        articleBean2.setArticleId(2);
        articleBean2.setTitle("标题2");
        articleBean2.setDescription("描述2");
        articleBean2.setTime("2017-4-2");
        articleBean2.setImgid(R.drawable.f2);
        list.add(articleBean2);

        ArticleBean articleBean3 = new ArticleBean();
        articleBean3.setArticleId(3);
        articleBean3.setTitle("标题3");
        articleBean3.setDescription("描述");
        articleBean3.setTime("2017-4-3");
        articleBean3.setImgid(R.drawable.f3);
        list.add(articleBean3);

        ArticleBean articleBean4 = new ArticleBean();
        articleBean4.setArticleId(4);
        articleBean4.setTitle("标题4");
        articleBean4.setDescription("描述4");
        articleBean4.setTime("2017-4-4");
        articleBean4.setImgid(R.drawable.f4);
        list.add(articleBean4);

        ArticleBean articleBean5 = new ArticleBean();
        articleBean5.setArticleId(5);
        articleBean5.setTitle("标题5");
        articleBean5.setDescription("描述5");
        articleBean5.setTime("2017-4-5");
        articleBean5.setImgid(R.drawable.f5);
        list.add(articleBean5);

        ArticleBean articleBean6 = new ArticleBean();
        articleBean6.setArticleId(6);
        articleBean6.setTitle("标题6");
        articleBean6.setDescription("描述6");
        articleBean6.setTime("2017-4-6");
        articleBean6.setImgid(R.drawable.f6);
        list.add(articleBean6);

        return list;
    }*/
}
